package com.qf.Dao.Impl;/*
 *
 */

import com.qf.Dao.CartDao;
import com.qf.Domain.Cart;
import com.qf.Utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private QueryRunner qr=new QueryRunner();
    @Override
    public void insert(Cart cart) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="insert into tb_cart(id,pid,num,money)values(?,?,?,?)";
        Object[] params={cart.getId(),cart.getPid(),cart.getNum(),cart.getMoney()};
        try {
            qr.update(conn, sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public List<Cart> select(Integer uid) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="select * from tb_cart where id=?";
        try {
            return qr.query(conn, sql,new BeanListHandler<>(Cart.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public Cart selectByUidAndPid(Integer uid, Integer pid) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="select * from tb_cart where id=? and pid=?";
        try {
            return qr.query(conn, sql,new BeanHandler<>(Cart.class),uid,pid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public void updata(Cart cart) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="update tb_cart set num=?,money=? where id=? and pid=?";
        Object[] params={cart.getNum(),cart.getMoney(),cart.getId(),cart.getPid()};
        try {
            qr.update(conn, sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public void delete(Integer uid, int pid) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="delete from tb_cart where id=? and pid=?";
        try {
            qr.update(conn, sql,uid,pid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public void delete(Integer uid) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="delete from tb_cart where id=?";
        try {
            qr.update(conn, sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }
}
